﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xajhzwb.Components.Data;

namespace Xajhzwb.Components.Publish
{
    public class UploadManager
    {
        private List<UploadTask> m_ListUpload;
        public List<UploadTask> ListUpload
        {
            get { return m_ListUpload; }
        }

        public UploadManager()
        {
            m_ListUpload = new List<UploadTask>();
        }

        ~UploadManager()
        {
        }

        public void AddUploadTask(UploadTask pt)
        {
            ListUpload.Add(pt);
            TaskInit(pt);
            pt.startPublic();
        }

        private void TaskInit(UploadTask uploadTask)
        {
            uploadTask.UploadCompleted  += this.onUploadCompleted;
            uploadTask.UploadFailed  += this.onUploadFailed;
            uploadTask.UploadStarted  += this.onUploadStarted;
            uploadTask.UploadError  += this.onUploadError;
            uploadTask.UploadLog += this.onUploadLog;
            //pTask.RunTask += this.onRunQidorTask;
        }

        private void onUploadLog(object sender, UploadLogEventArgs e)
        {
            if (e_UploadLog != null && !e.Cancel)
            {
                e_UploadLog(sender, e);
            }
        }

        private void onUploadCompleted(object sender, UploadCompletedEventArgs e)
        {
            UploadTask pt = (UploadTask)sender;
            m_ListUpload.Remove(pt);
            pt = null;

            if (e_UploadCompleted != null && !e.Cancel)
            {
                e_UploadCompleted(sender, e);
            }

        }

        private void onUploadFailed(object sender, UploadFailedEventArgs e)
        {
            UploadTask pt = (UploadTask)sender;
            m_ListUpload.Remove(pt);
            pt = null;

            if (e_UploadFailed != null && !e.Cancel)
            {
                e_UploadFailed(sender, e);
            }

        }

        private void onUploadStarted(object sender, UploadStartedEventArgs e)
        {
            if (e_UploadStarted != null && !e.Cancel)
            {
                e_UploadStarted(sender, e);
            }
        }

        private void onUploadError(object sender, UploadErrorEventArgs e)
        { 
            UploadTask pt = (UploadTask)sender;
            m_ListUpload.Remove(pt);
            pt = null;

            if (e_UploadError != null && !e.Cancel)
            {
                e_UploadError(sender, e);
            }

        }

        //private void onRunQidorTask(object sender, cRunTaskEventArgs e)
        //{
        //    e_RunTask(this, new RunTaskEventArgs(e.MessType, e.RunName, e.RunPara));
        //}

        #region 事件

        private event EventHandler<UploadCompletedEventArgs> e_UploadCompleted;
        public event EventHandler<UploadCompletedEventArgs> UploadCompleted
        {
            add { e_UploadCompleted += value; }
            remove { e_UploadCompleted -= value; }
        }

        private event EventHandler<UploadFailedEventArgs> e_UploadFailed;
        public event EventHandler<UploadFailedEventArgs> UploadFailed
        {
            add { e_UploadFailed += value; }
            remove { e_UploadFailed -= value; }
        }

        private event EventHandler<UploadStartedEventArgs> e_UploadStarted;
        public event EventHandler<UploadStartedEventArgs> UploadStarted
        {
            add { e_UploadStarted += value; }
            remove { e_UploadStarted -= value; }
        }

        private event EventHandler<UploadErrorEventArgs> e_UploadError;
        public event EventHandler<UploadErrorEventArgs> UploadError
        {
            add { e_UploadError += value; }
            remove { e_UploadError -= value; }
        }

        private event EventHandler<UploadLogEventArgs> e_UploadLog;
        public event EventHandler<UploadLogEventArgs> UploadLog
        {
            add { e_UploadLog += value; }
            remove { e_UploadLog -= value; }
        }

        //private event EventHandler<cRunTaskEventArgs> e_RunTask;
        //internal event EventHandler<cRunTaskEventArgs> RunTask
        //{
        //    add { e_RunTask += value;  }
        //    remove { e_RunTask -= value;  }
        //}

        #endregion

    }
}
